package com.sky.controller.user;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.Dishservice;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
@Api(tags = "菜品管理")
public class DishController {
    @Autowired
    Dishservice dishservice;
    @Autowired
    RedisTemplate redisTemplate;
    //根据分类ID查询套餐菜品
    @GetMapping("/list")
    public Result listByCategoryId(Long categoryId){
        //查询redis缓存中的菜品数据列表
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get("dish_" + categoryId);
        //如果缓存中查到菜品数据列表 直接返回给小程序客户端
        if (list!=null && list.size()>0){
            return Result.success(list);
        }
        //如果没有命中缓存 则正常查询mysql中的菜品列表
        list = dishservice.listByCategoryId(categoryId);
        //将mysql查询到的菜品同步到dishe缓存中
        redisTemplate.opsForValue().set("dish_" + categoryId,list);
        return Result.success(list);
    }

}
